package com.engc.stumng.web.newStudent;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.engc.common.service.BaseService;
import com.engc.common.utils.BaseUtils;

/**
 * 用户信息查询
 * 
 *
 */
@Controller
@RequestMapping("/user")
public class UserController
{
	
	@Resource
	private BaseService baseService;
	
	@RequestMapping("/view")
	public String view(HttpServletRequest request)
	{
		return "user/user";
	}
	
	@RequestMapping("/changeview")
	public String changeview(HttpServletRequest request)
	{
		return "user/user_change";
	}
	
	@RequestMapping("/getUsers")
	@ResponseBody
	public List<Map<String, Object>> getUser(HttpServletRequest request)
	{
		String sql = "select t.usercode id,t.username text from stb_customer t";
		
		List<Map<String, Object>> list = baseService.Query(sql);
		
		return list;
	}
	
	@RequestMapping("/getClasses")
	@ResponseBody
	public List<Map<String, Object>> getClasses(HttpServletRequest request)
	{
		String sql = "select t.id id,t.name text from t_study_info t ";
		
		List<Map<String, Object>> list = baseService.Query(sql);
		
		return list;
	}
	
	
	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> insert(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getInsertParam(request);
		
		
		try {
			baseService.Insert("stb_customer", record);
			return BaseUtils.getSuccessMessage("新增成功");
		} catch (Exception e) {
			return BaseUtils.getErrorMessage("新增失败,用户名已存在");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request) {
		Map<String, Object> record = BaseUtils.getUpdateParam(request);
		
		
		try {
			baseService.Update("stb_customer", record, "usercode=?", record.get("usercode"));
			return BaseUtils.getSuccessMessage("修改成功");
		} catch (Exception e) {
			return BaseUtils.getErrorMessage("修改失败");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request) {
		String usercode = request.getParameter("usercode");
		
		try {
			baseService.Delete("stb_customer", "usercode=?",usercode);
			return BaseUtils.getSuccessMessage("删除成功");
		} catch (Exception e) {
			return BaseUtils.getErrorMessage("删除失败");
		}
	}
}
